package org.itzheng.ui;

import android.widget.ImageView;

import com.bumptech.glide.Glide;

import org.itzheng.itplayer.bean.VideoFolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.Formatter;
import java.util.Locale;

/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2020-12-7.
 */
public class ViewShowUtils {


    public static void showImage(@Nullable ImageView imageView, @Nullable String path) {
        Glide.with(imageView.getContext())
                .load(path)
                .into(imageView);
    }

    /**
     * 将长度转换为时间
     *
     * @param timeMs
     * @return
     */
    public static String stringForTime(long timeMs) {
        //将长度转换为时间
        StringBuilder mFormatBuilder = new StringBuilder();
        Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
        long totalSeconds = timeMs / 1000;
        long seconds = totalSeconds % 60;
        long minutes = totalSeconds / 60 % 60;
        long hours = totalSeconds / 3600;
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

    /**
     * 获取文件夹名字
     *
     * @param path
     * @return
     */
    @NotNull
    public static String getFolderName(@Nullable String path) {
        File file = new File(path);
        if (file.exists()) {
            return file.getName();
        }
        return path;
    }

    /**
     * 文件的图标，
     *
     * @param ivIcon
     * @param item
     */
    public static void showFolderIcon(@Nullable ImageView ivIcon, @NotNull VideoFolder item) {
        if (item.fContent == null || item.fContent.isEmpty()) {
            showImage(ivIcon, "");
        } else {
            showImage(ivIcon, item.fContent.get(0).fPath);
        }
    }
}
